package com.finance3.springmvc.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

import com.finance3.springmvc.bean.User;
import com.finance3.springmvc.util.JDBCUtils;

public class queryUserImple implements queryUser{

	@Override
	public List<User> queryUser(String uid,String upw) {
		//User user = new User();
		//Map<String,Object> user=new HashMap<>();
		//List user1 = new ArrayList<>();
		Connection conn=null;
		ResultSet rs=null;
		try {
			
			conn = JDBCUtils.getConnection();
			//QueryRunner runner = new QueryRunner();
			String sql = "select id,uname,uid,upw,surplus from user where uid=? and upw=?";
			PreparedStatement ps = conn.prepareStatement(sql);
			
			//System.out.println(uid+"\n"+upw);
			
			ps.setString(1, uid);
			ps.setString(2, upw);
			rs = ps.executeQuery();
			List<User> allUser = new ArrayList<>();
			//BeanListHandler<User> params=new BeanListHandler<>(User.class);
			//try {
				//List<User> querylist = runner.query(conn, sql,uid,upw);
//			user.put("id", user.get(1));
//			user.put("uname",user.get(2));
//			user.put("uid", user.get(3));
//			user.put("upw",user.get(4));
//			user.put("surplus", user.get(5));
				//return querylist;
			//} catch (SQLException e) {
				// TODO Auto-generated catch block
				//e.printStackTrace();
			//}finally {
			//	JDBCUtils.closeResource(conn, null, null);
			//}
			while(rs.next()) {
				User user = new User();
				user.setId(rs.getInt("id"));
				user.setUname(rs.getString("uname"));
				user.setUid(rs.getString("uid"));
				user.setUpw(rs.getString("upw"));
				user.setSurplus(rs.getDouble("surplus"));
				allUser.add(user);
			}
			
			//System.out.println(allUser);
			
			return allUser;
			
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			JDBCUtils.closeResource3(conn, rs);
		}
		return null;
	}

	@Override
	public User getUser(String uid, String upw) {
		Connection conn=null;
		ResultSet rs=null;
		User user = null;
		//System.out.println(user==null?"为null":"不为null");
		try {
			conn = JDBCUtils.getConnection();
			String sql = "select id,uname,uid,upw,surplus from user where uid=? and upw=?";
			PreparedStatement ps = conn.prepareStatement(sql);
			
//			System.out.println("uid: "+uid);
//			System.out.println("upw: "+upw);
			
			ps.setString(1, uid);
			ps.setString(2, upw);
			rs = ps.executeQuery();
			//User user = new User();
			//System.out.println(user);
			//System.out.println(user==null?"为null":"不为null");
			while(rs.next()) {
				user = new User();
				user.setId(rs.getInt("id"));
				user.setUname(rs.getString("uname"));
				user.setUid(rs.getString("uid"));
				user.setUpw(rs.getString("upw"));
				user.setSurplus(rs.getDouble("surplus"));
			}
			//System.out.println(user+" 查询之后的user对象");
			return user;
			
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			JDBCUtils.closeResource3(conn, rs);
		}
		return null;
	}
	
	@Override
	public String getUserByUid(String uid) {
		User user=null;
		Connection conn=null;
		try {
			String sql = "select uid from user where uid=?";
			conn = JDBCUtils.getConnection();
			QueryRunner runner = new QueryRunner();
			user = runner.query(conn, sql,new BeanHandler<User>(User.class), uid);
			
			//System.out.println("queryUserImple里面的通过uid查询的uid的值:\n"+userid.get(uid));
			
			//String usid=(String)userid.get(uid);
			if(user!=null) {
				return user.getUid();
			}
			else {
				return null;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			JDBCUtils.closeResource2(conn);
		}
		return null;
	}
	
	/**
	 * 修改密码
	 * 2020-10-04_20:05
	 * 阴雨
	 */
	@Override
	public int alertUser(String uid, String password) {
		Connection conn=null;
		try {
			conn=JDBCUtils.getConnection();
			String sql = "update user set upw=? where uid=?";
			QueryRunner runner = new QueryRunner();
			int i = runner.update(conn, sql, password,uid);
			return i;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			JDBCUtils.closeResource2(conn);
		}
		return 0;
		
	}

}
